package com.xinchao.filter;


import com.alibaba.fastjson.JSONObject;
import com.xinchao.cache.RedisCacheDao;
import com.xinchao.constant.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录过滤器
 *
 * @author dxy
 * @date 2018/9/15 12:39
 */
public class LoginFilter implements Filter {
	private RedisCacheDao redisCacheDao;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		ServletContext servletContext = filterConfig.getServletContext();
		WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		redisCacheDao = (RedisCacheDao) webApplicationContext.getBean("redisCacheDao");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		//获取平台参数
		String projectType = httpRequest.getParameter(RequestConstant.PARAMETER_PROJECT_TYPE);
		if (projectType == null || projectType == "") {
			String content = "请输入平台参数";
			generateRespnseContent(httpResponse, content);
			return;
		}
		//如果是播控助手来请求数据，需要校验登录
		if (String.valueOf(ProjectTypeConstant.PROJECT_TYPE_BKZS).equals(projectType)) {
			//contextPath
			String contextPath = httpRequest.getContextPath();
			//requestURI
			String requestURI = httpRequest.getRequestURI();
			//放行登录接口
			if (requestURI.equals(contextPath + ApiURIConstant.API_URI_LOGIN)) {
				chain.doFilter(httpRequest, httpResponse);
			} else {
				//用户名
				String userName = httpRequest.getParameter(JSONConstant.JOSN_KEY_USER_NAME);
				//token标识
				String token = httpRequest.getParameter(JSONConstant.JOSN_KEY_TOKEN);
				if (userName == null || userName == "" || token == null || token == "") {
					String content = "请输入用户名或token";
					generateRespnseContent(httpResponse, content);
					return;
				}
				//从redis缓存中查出用户的token标识
				Object cacheTokenObj = redisCacheDao.getCache(CacheConstant.CACHE_NAME_TOKEN, userName);
				if (!token.equals(cacheTokenObj)) {
					String content = "用户没有登录，请登录";
					generateRespnseContent(httpResponse, content);
					return;
				}
				chain.doFilter(httpRequest, httpResponse);
			}
		} else if (String.valueOf(ProjectTypeConstant.PROJECT_TYPE_MIPS).equals(projectType)){
			//如果是mips平台来请求数据，不需要校验登录
			chain.doFilter(httpRequest, httpResponse);
		} else {
			String content = "无效数据";
			generateRespnseContent(httpResponse, content);
			return;
		}
	}

	@Override
	public void destroy() {

	}

	/**
	 * 生产响应内容
	 * @param httpResponse HttpServletResponse
	 * @param content 提示内容
	 * @throws IOException
	 */
	private void generateRespnseContent(HttpServletResponse httpResponse, String content) throws IOException {
		//封装返回信息
		JSONObject jsonObject = new JSONObject();
		jsonObject.put(ResponseConstant.RESPONSE_JSON_DATA, "");
		jsonObject.put(ResponseConstant.RESPONSE_JSON_MSG, content);
		jsonObject.put(ResponseConstant.RESPONSE_JSON_CODE, ResponseConstant.CODE_FAIL);
		//写入ServletOutputStream
		ServletOutputStream outputStream = httpResponse.getOutputStream();
		outputStream.write(jsonObject.toJSONString().getBytes());
		outputStream.flush();
	}
	
}
